<?php
/*
 * litchicms
 * Copyright (C) 2010-5-13 Author: tiefa(539247@qq.com)
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

!defined('BRK') && exit("!");

class Tree {
    public $data = array();
    public $child = array(-1=>array());
    public $layer = array(-1=>-1);
    public $parent = array();

    private $hasbeenLayer=FALSE;

    public  function __construct (&$data,$id,$parent,$value) {
        $this->setNode(0, -1, '__root__');
        foreach ($data as $k=>$v) {
            $this->setNode($v[$id], $v[$parent], $v[$value]);
        }
    }

    public function setNode($id, $parent, $value) {

        $parent = $parent?$parent:0;
        $this->data[$id] = $value;
        if(!key_exists($id, $this->child)) $this->child[$id]=array();
        $this->child[$parent][] = $id;
        $this->parent[$id] = $parent;
    }

    public function orderLayer() {
        if($this->hasbeenLayer)  return;
        foreach ($this->data as $k=>$v)
            $this->changeLayer($k);
        $this->hasbeenLayer=TRUE;
    }
    private function changeLayer($k) {
        if(!key_exists($this->parent[$k], $this->layer))
            $this->changeLayer($this->parent[$k]);
        $this->layer[$k]=$this->layer[$this->parent[$k]]+1;
    }

    public function getList (&$cat, $root= 0) {
        foreach ($this->child[$root] as $key=>$id) {
            $cat[] = $id;
            if ($this->child[$id]) $this->getList($cat, $id);
        }
    }
    public function getValue ($id) {
        return $this->data[$id];
    }
    public function getLayer ($id, $space = false , $start=0) {
        return $space?($this->layer[$id]-$start<=0?'':str_repeat($space,$this->layer[$id]-$start)):$this->layer[$id];
    }
    public function getParent ($id) {
        return $this->parent[$id];
    }
    public function getParents ($id) {
        $this->orderLayer();
        while ($this->parent[$id] != -1) {
            $id = $parent[$this->layer[$id]] = $this->parent[$id];
        }
        ksort($parent);
        reset($parent);
        return $parent;
    }
    public function getChild ($id = 0) {
        return $this->child[$id];
    }
    public function getChilds ($id = 0) {
        $this->orderLayer();
        $child = array($id);
        $this->getList($child, $id);
        return $child;
    }

}
?>